package cn.dceast.situation.controller;


import cn.dceast.common.data.RtnData;
import cn.dceast.common.utils.Md5Utils;
import cn.dceast.common.utils.StringUtil;
import cn.dceast.situation.service.SituationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/record")
public class SituationController {

    public static final Logger logger = LoggerFactory.getLogger(SituationController.class);

    @Autowired
    private SituationService situationService;

    @RequestMapping(value = "/myRecordList.json", method = RequestMethod.GET)
    @ResponseBody
    public RtnData getMyRecordList(@RequestParam(value = "pageIndex",defaultValue = "0" , required = false) int pageIndex,
                                   @RequestParam(value = "pageSize",defaultValue = "10", required = false) int pageSize,
                                   @RequestParam(required = false) String startDate,
                                   @RequestParam(required = false) String endDate,
                                   @RequestParam(required = true) int userId){
        Map<String,Object> retMap = new HashMap<String, Object>();
        Map<String, Object> params = new HashMap<String, Object>();
        try {
            if (!StringUtil.isEmpty(startDate)){
                startDate = startDate+" 00:00:00";
                params.put("startDate", startDate);
            }
            if (!StringUtil.isEmpty(endDate)){
                endDate = endDate+" 23:59:59";
                params.put("endDate", endDate);
            }
            if(pageIndex == 0){
                params.put("startIndex",pageIndex);
            }else {
                params.put("startIndex", (pageIndex - 1) * pageSize);
            }
            params.put("pageSize",pageSize);
            params.put("userId",userId);
            retMap.put("list",situationService.getListByUserId(params));
            retMap.put("count",situationService.getCountByUserId(params));
            return RtnData.ok(retMap);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("========查询我的轨迹列表失败=========",e);
        }
        return RtnData.fail("查询我的轨迹列表失败");
    }
}